// closure to avoid namespace collision
(function(){
	// creates the plugin
	tinymce.create('tinymce.plugins.dlprotect', {
		// creates control instances based on the control's id.
		// our button's id is "dlprotect_button"
		createControl : function(id, controlManager) {
			if (id == 'dlprotect_button') {
				// creates the button
				var button = controlManager.createButton('dlprotect_button', {
					title : 'Download Protect', // title of the button
					image : '../wp-content/plugins/download-protect/resources/images/download_protect.png',  // path to the button's image
					onclick : function() {
						// triggers the thickbox
						var width = jQuery(window).width(), H = jQuery(window).height(), W = ( 720 < width ) ? 720 : width;
						W = W - 80;
						H = H - 84;
						tb_show( 'Download Protect', '#TB_inline?width=' + W + '&height=' + H + '&inlineId=dlprotect-form' );
					}
				});
				return button;
			}
			return null;
		}
	});
	
	// registers the plugin. DON'T MISS THIS STEP!!!
	tinymce.PluginManager.add('dlprotect', tinymce.plugins.dlprotect);
	
	// executes this when the DOM is ready
	jQuery(function(){
		/* var data = {
				action: 'dlprotect_editor',
				whatever: 'test'
			};
		var tester = jQuery.post(ajaxurl, data, function(response) {
		}); */
		
		var form = jQuery('<div id="dlprotect-form"><table id="dlprotect-table" class="form-table">\
			<tr>\
			<th><label for="dlprotect-file">File</label></th>\
			<td><input type="text" id="dlprotect-file" name="file" value="" /><br />\
			<small>Choose the file to download</small></td>\
		</tr>\
		<tr>\
			<th><label for="dlprotect-visibility">Visibility</label></th>\
			<td><select name="visibility" id="dlprotect-visibility" value="">\
				<option value="private">Private</option>\
				<option value="public">Public</option>\
				</select><br />\
			<small>Choose whether to make this file private or not.</small></td>\
		</tr>\
		<tr>\
			<th><label for="dlprotect-linktext">Link Text</label></th>\
			<td><input type="text" name="linktext" id="dlprotect-linktext" value="" /><br />\
			<small>The text displayed to download this file.</small></td>\
		</tr>\
	</table>\
	<p class="submit">\
		<input type="button" id="dlprotect-submit" class="button-primary" value="Insert File" name="submit" />\
	</p>\
	</div>');
	
		var table = form.find('table');
		form.appendTo('body').hide();
		
		// handles the click event of the submit button
		form.find('#dlprotect-submit').click(function(){
			// defines the options and their default values
			// again, this is not the most elegant way to do this
			// but well, this gets the job done nonetheless
			var options = { 
				'file'    : '',
				'visibility'         : ''
				};
			var shortcode = '[dlprotect';
			
			for( var index in options) {
				var value = table.find('#dlprotect-' + index).val();
				
				// attaches the attribute to the shortcode only if it's different from the default value
				if ( value !== options[index] )
					shortcode += ' ' + index + '="' + value + '"';
			}
			
			shortcode += ']';
			shortcode += table.find('#dlprotect-linktext').val();
			shortcode += '[/dlprotect]';
			
			// inserts the shortcode into the active editor
			tinyMCE.activeEditor.execCommand('mceInsertContent', 0, shortcode);
			
			// closes Thickbox
			tb_remove();
		});
	});
})()